@extends('admin.common.main')
@section('css')
    {{--  引入webuploader css 异步文件上传  --}}
    <link rel="stylesheet" type="text/css" href="/webuploader/webuploader.css">
@endsection
@section('cnt')
    <nav class="breadcrumb">
        <i class="Hui-iconfont">&#xe67f;</i> 首页
        <span class="c-gray en">&gt;</span> 房源管理
        <span class="c-gray en">&gt;</span> 房东修改
        <a class="btn btn-success radius r" style="line-height:1.6em;margin-top:3px"
           href="javascript:location.replace(location.href);" title="刷新">
            <i class="Hui-iconfont">&#xe68f;</i>
        </a>
    </nav>

    <article class="page-container">

        <form id="form-member-add" action="{{ route('admin.fangowner.update',$fangowner) }}"
              class="form form-horizontal">
            @csrf
            <input type="hidden" name="id" value="{{ $fangowner['id'] }}">
            <div class="row cl">
                <label class="form-label col-xs-4 col-sm-3">
                    <span class="c-red">*</span>房东姓名：
                </label>
                <div class="formControls col-xs-8 col-sm-9">
                    <input type="text" class="input-text" name="name" value="{{ $fangowner['name'] }}">
                </div>
            </div>
            <div class="row cl">
                <label class="form-label col-xs-4 col-sm-3">
                    <span class="c-red">*</span>性别：
                </label>
                <div class="formControls col-xs-8 col-sm-9">
                    <input type="radio" name="gender" value="男" @if($fangowner['gender'] == '男') checked
                           @endif @click="get_gender('男')">男士
                    <input type="radio" name="gender" @if($fangowner['gender'] == '女') checked @endif value="女"
                           @click="get_gender('女')">女士
                </div>
            </div>
            <div class="row cl">
                <label class="form-label col-xs-4 col-sm-3">
                    <span class="c-red">*</span>年龄：
                </label>
                <div class="formControls col-xs-8 col-sm-9">
                    <input type="text" class="input-text" name="age" value="{{ $fangowner['age'] }}">
                </div>
            </div>
            <div class="row cl">
                <label class="form-label col-xs-4 col-sm-3">
                    <span class="c-red">*</span>手机号：
                </label>
                <div class="formControls col-xs-8 col-sm-9">
                    <input type="text" class="input-text" name="phone" value="{{ $fangowner['phone'] }}">
                </div>
            </div>
            <div class="row cl">
                <label class="form-label col-xs-4 col-sm-3">
                    <span class="c-red">*</span>地址：
                </label>
                <div class="formControls col-xs-8 col-sm-9">
                    <input type="text" class="input-text" name="address" value="{{ $fangowner['address'] }}">
                </div>
            </div>
            <div class="row cl">
                <label class="form-label col-xs-4 col-sm-3">
                    <span class="c-red">*</span>身份证号：
                </label>
                <div class="formControls col-xs-8 col-sm-9">
                    <input type="text" class="input-text" name="card" value="{{ $fangowner['card'] }}">
                </div>
            </div>
            <div class="row cl">
                <label class="form-label col-xs-4 col-sm-3">身份证照片：
                </label>
                <div class="formControls col-xs-8 col-sm-9">
                    <input type="hidden" class="input-text" id="pic" name="pic" value="{{ $fangowner['pic'] }}">
                    <input type="hidden" class="input-text" id="pic1" name="pic1" value="{{ $fangowner['pic1'] }}">
                    <input type="hidden" class="input-text" id="pic2" name="pic2" value="{{ $fangowner['pic2'] }}">
                    <div id="fileList" class="uploader-list">
                        <img src="{{ $fangowner['pic'] }}" id="img" style="width: 160px;height: 100px">
                        <strong class="delimg" onclick="delimg(1)">X</strong>
                        <img src="{{ $fangowner['pic1'] }}" id="img1" style="width: 160px;height: 100px">
                        <strong class="delimg" onclick="delimg(2)">X</strong>
                        <img src="{{ $fangowner['pic2'] }}" id="img2" style="width: 160px;height: 100px">
                        <strong class="delimg" onclick="delimg(3)">X</strong>
                    </div>
                    <br>
                    <span class="filePicker" onclick="add_id(1)">正面</span>
                    <span class="filePicker" onclick="add_id(2)">反面</span>
                    <span class="filePicker" onclick="add_id(3)">手持</span>
                </div>
            </div>
            <div class="row cl">
                <label class="form-label col-xs-4 col-sm-3">
                    <span class="c-red">*</span>邮箱：
                </label>
                <div class="formControls col-xs-8 col-sm-9">
                    <input type="text" class="input-text" name="email" value="{{ $fangowner['email'] }}">
                </div>
            </div>

            <div class="row cl">
                <div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-3">
                    <input class="btn btn-primary radius" type="submit" value="修改节点">
                </div>
            </div>
        </form>
    </article>

    <style>
        .filePicker {
            margin-right: 60px;
            margin-left: 50px;
        }

        .delimg {
            cursor: pointer;
        }
    </style>
@endsection

@section('js')
    {{--// 引入验证的jquery--}}
    <script type="text/javascript" src="/admin/lib/My97DatePicker/4.8/WdatePicker.js"></script>
    <script type="text/javascript" src="/admin/lib/datatables/1.10.0/jquery.dataTables.min.js"></script>
    <script type="text/javascript" src="/admin/lib/laypage/1.2/laypage.js"></script>
    <script type="text/javascript" src="/admin/lib/jquery.validation/1.14.0/jquery.validate.js"></script>
    <script type="text/javascript" src="/admin/lib/jquery.validation/1.14.0/validate-methods.js"></script>
    <script type="text/javascript" src="/admin/lib/jquery.validation/1.14.0/messages_zh.js"></script>

    {{-- webuploader js 异步文件上传 --}}
    <script type="text/javascript" src="/webuploader/webuploader.js"></script>

    <script>


        function delimg(aa) {
            if (aa == 1) {
                let url = "{{ route('admin.fangowner.delimg') }}?src=" + '{{ $fangowner['pic'] }}';
                fetch(url);
                $('#pic').val('{{ config('uploadpic.img') }}');
                $('#img').attr('src', '{{ config('uploadpic.img') }}');
            } else if (aa == 2) {
                let url = "{{ route('admin.fangowner.delimg') }}?src=" + '{{ $fangowner['pic1'] }}';
                fetch(url);
                $('#pic1').val('{{ config('uploadpic.img') }}');
                $('#img1').attr('src', '{{ config('uploadpic.img') }}');
            } else if (aa == 3) {
                let url = "{{ route('admin.fangowner.delimg') }}?src=" + '{{ $fangowner['pic2'] }}';
                fetch(url);
                $('#pic2').val('{{ config('uploadpic.img') }}');
                $('#img2').attr('src', '{{ config('uploadpic.img') }}');
            } else {
                let url = "{{ route('admin.fangowner.delimg') }}?src=" + '{{ $fangowner['pic'] }}';
                fetch(url);
                $('#pic').val('{{ config('uploadpic.img') }}');
                $('#img').attr('src', '{{ config('uploadpic.img') }}');
            }
        }

        $("#form-member-add").validate({
            rules: {
                name: {
                    required: true
                },
                pid: {
                    required: true
                },
                icon: {
                    // 密码一致 需是ID
                    required: true
                },
            },
            onkeyup: false,
            success: "valid",
            submitHandler: function (form) {
                // 验证成功 提交表单
                let url = $(form).attr('action');
                let data = $(form).serialize();
                $.ajax({
                    url,
                    data,
                    type: "PUT",
                }).then(({msg, status}) => {
                    if (status == 200) {
                        layer.msg(msg, {icon: 1, time: 900});
                        location.href = '{{ route('admin.fangowner.index') }}';
                    } else {
                        layer.msg(msg, {icon: 2, time: 900});
                    }
                })
            },
        });

        // webupload
        var uploader = WebUploader.create({
            // 选完文件后，是否自动上传。
            auto: true,
            // swf文件路径
            swf: '/js/Uploader.swf',
            // post上传的携带的数据
            formData: {
                _token: "{{ csrf_token() }}"
            },
            // 文件接收服务端。
            server: '{{ route('admin.fangowner.upimg') }}',
            // 内部根据当前运行是创建，可能是input元素，也可能是flash.
            pick: {
                id: '.filePicker',
                // 是否允许同时上传多个文件
                multiple: false,
            },
            // 只允许选择图片文件。
            accept: {
                title: 'Images',
                extensions: 'gif,jpg,jpeg,bmp,png',
                mimeTypes: 'image/*'
            },
        });

        var id = 0;

        function add_id(a) {
            id = a;
        }

        // 图片上传成功
        uploader.on('uploadSuccess', (file, ret) => {

            // console.log(ret.src);
            if (id == 1) {
                $('#pic').val(ret.src);
                $('#img').attr('src', ret.src);
            } else if (id == 2) {
                $('#pic1').val(ret.src);
                $('#img1').attr('src', ret.src);
            } else if (id == 3) {
                $('#pic2').val(ret.src);
                $('#img2').attr('src', ret.src);
            } else {
                console.log(id);
                $('#pic').val(ret.src);
                $('#img').attr('src', ret.src);
            }
        });
    </script>
@endsection
